package com.gx.smart.building.ui.swap.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.building.admin.ScheduleAppApiService
import com.gx.smart.building.ui.common.viewmodel.CommonViewModel
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordDetailResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.AppScheduleRecordResp
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordDto

/**
 *@author: xiaosy
 *时间: 2022/9/2
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class MySwapWorkViewModel(app: Application) : CommonViewModel(app) {
    private val detail: MutableLiveData<RecordDto> = MutableLiveData()
    private val checkSuccess: MutableLiveData<Boolean> = MutableLiveData()

    //交接班——详情查看
    fun queryAppScheduleRecordDetail(id: String): LiveData<RecordDto> {
        ScheduleAppApiService.getInstance().queryAppScheduleRecordDetail(id,
            object : CallBack<AppScheduleRecordDetailResp>() {
                override fun callBack(result: AppScheduleRecordDetailResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        detail.value = result.content
                    } else {
                        handleServiceException(comResp)
                    }
                }
            })
        return detail
    }

    //交接班动作——接班确认（返回成功与否）
    fun checkAppScheduleRecord(content: RecordDto): LiveData<Boolean> {
        ScheduleAppApiService.getInstance().checkAppScheduleRecord(content,
            object : CallBack<AppScheduleRecordResp>() {
                override fun callBack(result: AppScheduleRecordResp?) {
                    if (result == null) {
                        ToastUtils.showLong("服务器超时")
                        checkSuccess.value = false
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        checkSuccess.value = true
                    } else {
                        handleServiceException(comResp)
                        checkSuccess.value = false
                    }
                }

            })
        return checkSuccess
    }
}
